package com.suncnpap.system.controller;

import com.suncnpap.system.model.DuplicateRecord;
import com.suncnpap.system.model.School;
import com.suncnpap.system.service.DuplicateRecordService;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.app.util.common.FileUtil;
import com.app.util.common.HandlerResult;
import com.app.util.common.PathUtil;
import com.app.util.common.ResponseUtil;
import com.app.util.common.SysConfig;
import com.app.util.common.UuidUtil;
import com.app.util.database.mybatis.Page;
import com.suncnpap.base.common.ParamModel;
import com.suncnpap.base.controller.BaseController;
import com.suncnpap.datamanage.common.DataUtil;

/** 
 * 说明：备份记录Controller
 */
@Controller
@RequestMapping(value="/system/duplicateRecord")
public class DuplicateRecordController extends BaseController {
	
	@Autowired
	DuplicateRecordService duplicateRecordService;
	
	

	/**
	 * 去往列表页面
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/list")
	public void list(HttpServletRequest request, HttpServletResponse response) throws Exception{
	
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {

			Page page = this.setPage();
			page.removeQueryAnd("strSchoolId");
			page = duplicateRecordService.list(page);
			result = this.returnPage(result, page);
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	
	/**
	 * 添加处理
	 * @param obj
	 * @param response
	 */
	@RequestMapping(value="/add")
	public void add(@RequestBody DuplicateRecord obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				init(obj);
				duplicateRecordService.add(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		
		ResponseUtil.writeJson(result, response);
	}
	
	
	/**
	 * 修改处理
	 * @param strId
	 * @return
	 */
	@RequestMapping(value="/update",method=RequestMethod.POST)
	public void update(@RequestBody DuplicateRecord obj,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				duplicateRecordService.update(obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	/**
	 * 删除处理
	 * @param strId
	 * @return
	 */
	@RequestMapping(value="/delete",method=RequestMethod.POST)
	public void delete(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				if(strId != null && !strId.equals("")){
					duplicateRecordService.deleteMore(strId.split(","));
				}
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	@RequestMapping(value="/view")
	public void view(@RequestBody ParamModel paramModel,HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		if (result.getCode()==0) {
			try{
				String strId = paramModel.getStrId();
				DuplicateRecord obj = duplicateRecordService.findById(strId);
				result.put("obj", obj);
			}catch(Exception e){
				result.setCode(1);
				e.printStackTrace();
				throw e;
			}
		}
		ResponseUtil.writeJson(result, response);
	}
	
	@RequestMapping(value="/backup")
	public void backup(HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {
//			* @param hostIP ip地址，可以是本机也可以是远程
//		     * @param userName 数据库的用户名
//		     * @param password 数据库的密码
//		     * @param savePath 备份的路径
//		     * @param fileName 备份的文件名
//		     * @param databaseName 需要备份的数据库的名称
			String hostIP = SysConfig.getProString("MYSQL_HOST_IP");
			String userName = SysConfig.getProString("MYSQL_USERNAME");
			String password = SysConfig.getProString("MYSQL_PASSWORD");
			String savePath = PathUtil.getClasspath() + "/upload/backup/"+new SimpleDateFormat("yyyy-MM-dd").format(new Date())+"/";
			String fileName = UuidUtil.get32UUID()+".sql";
			String databaseName = SysConfig.getProString("MYSQL_DATABASENAME");
			Boolean flag = DataUtil.backup(hostIP, userName, password, savePath, fileName, databaseName);
			
			if(flag){
				DuplicateRecord obj = new DuplicateRecord();
				init(obj);
				obj.setStrName("plib");//数据库名  
				obj.setStrPath(savePath+fileName);				//备份路径 
				File f= new File(savePath+fileName);  
				obj.setStrSize(f.length()+"");
				duplicateRecordService.add(obj);
			}
			 
		}
		
		ResponseUtil.writeJson(result, response);

	}
	
	@RequestMapping(value="/recover")
	public void recover(@RequestBody ParamModel paramModel, HttpServletRequest request, HttpServletResponse response) throws Exception{
		
		HandlerResult result = this.checkToken(request);
		
		if (result.getCode()==0) {
			
			String strId = paramModel.getStrId();
			DuplicateRecord obj = duplicateRecordService.findById(strId);
//			* @param filepath 数据库备份的脚本路径
//		     * @param ip IP地址
//		     * @param database 数据库名称
//		     * @param userName 用户名
//		     * @param password 密码
			String filepath = obj.getStrPath();
			String hostIP = SysConfig.getProString("MYSQL_HOST_IP");
			String userName = SysConfig.getProString("MYSQL_USERNAME");
			String password = SysConfig.getProString("MYSQL_PASSWORD");
			String databaseName = SysConfig.getProString("MYSQL_DATABASENAME");
			
			DataUtil.recover(filepath,hostIP,databaseName, userName,password);
		}
		
		ResponseUtil.writeJson(result, response);

	}
}
